<?php

class Sqlserver {
	private $host = '';
	private $user = '';
	private $pwd  = '';
	
	//初始化SQLServer配置
	public function __construct() {
		$this->host = C('MS_HOST');
		$this->user = C('MS_USER');
		$this->pwd  = C('MS_PWD');
	}
	
	//解析函数
	public function __destruct() {
		$this->host = '';
		$this->user = '';
		$this->pwd  = '';
	}
	
	//执行语句
	public function query($sql) {
		$conn=odbc_connect($this->host,$this->user,$this->pwd);
		
		//连接失败
		if(!$conn) {
			echo "connection failed";
			return '';
		}
		
	//	echo microtime()."<br/>";
		$db=odbc_exec($conn,$sql);
	//	echo microtime();
		
		$result = array();
		while(@$temp = odbc_fetch_array($db)){
			if(!$temp) break;
				$result[] = $temp;
		}
		
		odbc_close($conn);
		
		return $result;
	}
	
	//查找多个结果
	public function select($sql) {
		$conn=odbc_connect($this->host,$this->user,$this->pwd);
		
		//连接失败
		if(!$conn) {
			echo "connection failed";
			return '';
		}
		
		$db=odbc_exec($conn,$sql);
		
		$result = array();
		while(@$temp = odbc_fetch_array($db)){
			if(!$temp) break;
				$result[] = $temp;
		}
		
		odbc_close($conn);
		
		return $result;
	}
	
	//查找单个结果
	public function find($sql) {
		$conn=odbc_connect($this->host,$this->user,$this->pwd);
		
		//连接失败
		if(!$conn) {
			echo "connection failed";
			return '';
		}
		
		$db=odbc_exec($conn,$sql);
		
		$result = array();
		while(@$temp = odbc_fetch_array($db)){
			if($temp) {
				$result = $temp;
				break;
			}
		}
		
		odbc_close($conn);
		
		return $result;
	}
	
	//执行操作，用于更新和删除
	public function update($sql) {
		$conn=odbc_connect($this->host,$this->user,$this->pwd);
		if(!$conn) {
			echo "connection failed";
			return false;
		}
		$db=odbc_exec($conn,$sql);
		return true;
	}
	
	//显示数据库连接情况
	public function xianshi() {
		echo $this->host;
		echo "<br/>";
		echo $this->user;
		echo "<br/>";
		echo $this->pwd;
	}
	


}

?>